package com.hq.fxh.No5_User.User.Fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.GetMyPostModle;
import com.hq.fxh.Modle.UserPlateModel;
import com.hq.fxh.No5_User.User.Adapter.Ladapter_GZ_Plate;
import com.hq.fxh.No5_User.User.Adapter.Ladapter_MyPlate;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.LogUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Subscriber;

import static cn.jpush.android.api.JPushInterface.a.e;
import static cn.jpush.android.api.JPushInterface.a.n;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/15
 * @Email xlwenhao1858@sina.com
 */


public class Fragment_MyPlate_Plate extends Fragment {
    protected View rootView;
    protected PullToRefreshListView listGZPList;
    private Ladapter_MyPlate adapter;
    List<UserPlateModel> plateData;
    int page = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_gz_people, null);

        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {

        listGZPList = (PullToRefreshListView) rootView.findViewById(R.id.list_GZP_List);
        initData();
        getMyPost();
    }

    private void initData() {
        adapter = new Ladapter_MyPlate();
        listGZPList.setMode(PullToRefreshBase.Mode.BOTH);
        listGZPList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                getMyPost();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = page + 1;
                getMyPost();
            }
        });

        adapter.setOnDeleteCallBack(new Ladapter_MyPlate.OnDeleteCallBack() {
            @Override
            public void deleteCall(int position, UserPlateModel data) {
                if (plateData == null) {

                } else {
                    int post_id = data.getPost_id();
                    String title = data.getTitle();
                    showNormalDialog(title, post_id, position);
                    //  deleteMyPlate(post_id, position);

                }
            }
        });

        listGZPList.setAdapter(adapter);
    }

    public static Fragment_MyPlate_Plate newInstance() {

        Bundle args = new Bundle();

        Fragment_MyPlate_Plate fragment = new Fragment_MyPlate_Plate();
        fragment.setArguments(args);
        return fragment;
    }


    public void getMyPost() {
        UserServiceImpl.GetMyPost(page, 20, new Callback<GetMyPostModle>() {
            @Override
            public void onResponse(Call<GetMyPostModle> call, Response<GetMyPostModle> response) {
                listGZPList.onRefreshComplete();
                if (response.body() == null) {
                    ResponseBody responseBody = response.errorBody();
                    String string = null;
                    try {
                        string = responseBody.string();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    LogUtils.e("errorbody信息" + string);
                } else {
                    try {
                        GetMyPostModle body = response.body();
                        List<UserPlateModel> data = body.getData();
                        if (data == null || data.size() == 0) {
                            LogUtils.startToash(getContext(), AppConstant.NullData);
                            return;
                        }

                        if (plateData == null) {
                            plateData = new ArrayList<>();
                        }
                        if (page == 1) {
                            plateData.clear();
                        }
                        plateData.addAll(data);

                        adapter.setData(plateData);
                        adapter.notifyDataSetChanged();
                        LogUtils.e("我的帖子信息" + body.toString());


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<GetMyPostModle> call, Throwable t) {
                listGZPList.onRefreshComplete();
                if (t != null) {
                    LogUtils.e("错误信息" + t.getMessage());
                }

            }
        });
    }

    public void deleteMyPlate(int postid, final int position) {
        UserServiceImpl.delMyPlate(postid, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                ResponseBody body = response.body();
                if (body == null) {
                    ResponseBody error = response.errorBody();
                    if (error == null) {
                        LogUtils.e(error + "空");

                    } else {
                        try {
                            LogUtils.e(error.toString());
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                } else {
                    try {

                        LogUtils.startToash(getContext(), body.string());

                        plateData.remove(position);
                        adapter.notifyDataSetChanged();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private void showNormalDialog(String title, final int posid, final int position) {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        if (title.length() > 5) {
            title = title.substring(0, 4);
        }

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(getContext());
        normalDialog.setIcon(R.mipmap.ldd_logo);
        normalDialog.setTitle("提示");
        normalDialog.setMessage("你确定要删除帖子‘" + title + "’?");
        normalDialog.setPositiveButton("删除",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteMyPlate(posid, position);
                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        // 显示
        normalDialog.show();
    }
}
